<template>
  <div class="set-field-permission">
    <el-dialog
      :title="$t('条件设置')"
      v-if="dialogVisible"
      :visible.sync="dialogVisible"
      width="50%"
      :before-close="handleClose"
      :append-to-body="true"
    >
      <div class="set-field-permission-content">
        <el-button icon="el-icon-plus" type="text" v-if="ruleList.length == 0" @click="handleAdd">添加</el-button>
        <div
          class="set-field-permission-content-item"
          v-for="(item, index) in ruleList"
          :key="index"
        >
          <div class="set-field-permission-content-item-condition">
            <el-row class="list-row">
              <el-col :span="12">
                <el-row>
                  <el-col :span="4">{{ $t('如果：') }}</el-col>
                  <el-col :span="20">
                    <el-input
                      @focus="handleCondion(item, index)"
                      v-model="item.ruleTextShow"
                      readonly
                      type="textarea"
                      :rows="4"
                      :placeholder="$t('请选择')"
                    ></el-input>
                  </el-col>
                </el-row>
              </el-col>
              <el-col :span="11" style="margin-left: 15px">
                <el-col :span="4">{{ $t('则为：') }}</el-col>
                <el-col :span="20">
                  <div class="right-title">
                    <span
                      >{{ $t('【') }}{{ row.pageName }}{{ $t('】 ')
                      }}{{ row.parameterName }}
                    </span>
                    <span class="title-tips">{{ $t('设置以下权限') }}</span>
                  </div>
                  <el-row style="margin-top: 20px">
                    <el-select v-model="item.selectValue">
                      <el-option
                        v-for="item in options"
                        :key="item.value"
                        :label="item.label"
                        :value="item.value"
                      ></el-option>
                    </el-select>
                  </el-row>
                </el-col>
              </el-col>
            </el-row>
          </div>
          <div class="set-field-permission-content-item-opertaion">
            <img
              @click="handleAdd(index)"
              src="@/assets/images/standard/plus_icon.png"
              alt=""
            />
            <img
              @click="handleDel(index)"
              src="@/assets/images/standard/minus_icon.png"
              alt=""
            />
          </div>
        </div>
      </div>
      <span slot="footer" class="dialog-footer">
        <el-button @click="handleClose">{{ $t('取 消') }}</el-button>
        <el-button type="primary" @click="confirm">{{ $t('确 定') }}</el-button>
      </span>
    </el-dialog>
    <setCondition
      v-if="optionsVisible"
      v-model="optionsVisible"
      :id="id"
      :currentIndex="currentIndex"
      :rowData="rowData"
      :itemData="itemData"
      @setCondition="setCondition"
    />
  </div>
</template>
<script>
import setCondition from './setCondition.vue'
import globalService from '@/services/globalService'
export default {
  components: {
    setCondition,
  },
  props: {
    value: Boolean,
    row: {
      type: Object,
      default: () => {
        return {}
      },
    },
    rowData: {
      type: Object,
      default: () => {
        return {}
      }
    },
    id: {},
  },
  data() {
    return {
      dialogVisible: false,
      ruleList: [{}],
      currentIndex: null,
      optionsVisible: false,
      itemData: {},
      otherRuleList: [],
      options: [],
    }
  },
  async created() {
    this.dialogVisible = this.value
    await this.setData()
  },
  methods: {
    async setData() {
      // 设置下拉值
      if (this.row.parameterValueStr) {
        const info = JSON.parse(this.row.parameterValueStr) || {}
        if (info.enumType == 2) {
          const type = info.relationDataType
          const templateTypeCode = this.row.templateTypeCode
          const res = await globalService.queryProjectDetailRelatedData({
            type,
            templateTypeCode,
          })
          this.options = (res.data.customerInfoList || []).map((it) => {
            return {
              ...it,
              value: it.customerCode || it.companyCode || it.mainUserId || it.id,
              label: it.optionName,
            }
          })
        } else {
          this.options = (info.optionsStr || []).map((it) => {
            return {
              ...it,
              value: it.optionName,
              label: it.optionDesc || it.optionName,
            }
          })
        }
      }
      if (this.row.ruleList && this.row.ruleList.length) {
        // 存储其他的规则
        this.otherRuleList = (this.row.ruleList || []).filter(
          (it) => it.conditionType != 2
        )
        // 取出当前规则
        let ruleList = (this.row.ruleList || []).filter(
          (it) => it.conditionType == 2
        )
        if (ruleList && ruleList.length == 0) {
          ruleList = [{}]
        }
        this.ruleList = (ruleList || []).map((it) => {
          // 设置选中状态
          const arr = (it.ruleValue && JSON.parse(it.ruleValue)) || []
          const info = (arr || []).find((it) => it.checked == 1)
          it.selectValue = info ? info.code : ''
          return {
            ...it,
          }
        })
      }
    },
    handleCondion(item, index) {
      this.currentIndex = index
      this.optionsVisible = true
      this.itemData = item
    },
    handleAdd(index) {
      this.ruleList.push({})
    },
    handleDel(index) {
      this.ruleList.splice(index, 1)
    },
    handleClose() {
      this.$emit('input', false)
    },
    // 条件设置
    setCondition(data) {
      const info = this.ruleList[data.currentIndex]
      this.$set(this.ruleList, data.currentIndex, {
        ...info,
        ...data,
      })
    },
    confirm() {
      const ruleList = (this.ruleList || []).map((it, idx) => {
        const arr = (this.options || []).map((item) => {
          return {
            code: item.value,
            name: item.label,
            checked: it.selectValue == item.value ? 1 : 0,
          }
        })
        return {
          conditionType: 2,
          ruleText: it.ruleText,
          ruleTextShow: it.ruleTextShow,
          ruleValue: JSON.stringify(arr),
          relatedRuleFieldList: it.relatedRuleFieldList,
        }
      })
      // 拼接所有规则
      const data = this.otherRuleList.concat(ruleList)
      this.$emit('setDefaultValue', data)
      this.handleClose()
    },
  },
}
</script>
<style lang="scss" scoped>
.set-field-permission {
  &-content {
    width: 100%;
    &-item {
      display: flex;
      justify-content: space-between;
      margin-bottom: 10px;
      &-condition {
        width: 90%;
        background: #faf9f9;
        border-radius: 4px;
        padding: 20px;
        box-sizing: border-box;
        .list-row {
          font-weight: 400;
          font-size: 14px;
          color: #333333;
          .title-tips {
            color: #999999;
            margin-left: 10px;
          }
        }
      }
      &-opertaion {
        width: 10%;
        padding: 20px;
        box-sizing: border-box;
        display: flex;
        flex-direction: column;
        justify-content: space-between;
        img {
          width: 32px;
          height: 32px;
          cursor: pointer;
        }
      }
    }
  }
}
</style>
